<template>
  <div class="selector-wrap">
    <div>
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input
            v-model="queryParams.keywords"
            placeholder="关键字"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="queryParams.type" :disabled="!!type" style="width: 100px" clearable>
            <el-option
              v-if="!withNorm"
              v-for="item in MaterialType"
              :key="item"
              :label="Material_LABEL[item]"
              :value="item"
            />
            <el-option
              v-else
              v-for="item in MaterialAllType"
              :key="item + '1'"
              :label="Material_LABEL[item]"
              :value="item"
            />
          </el-select>
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table :data="filteredMaterials" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="!operation" />
      <el-table-column label="材料名称" prop="name" width="150" />
      <el-table-column label="编码" prop="code" />
      <el-table-column label="规格" prop="spec" />
      <el-table-column label="市场价" width="120" align="right" prop="price">
        <template #default="scope">
          {{ `￥${scope.row.price}` }}
        </template>
      </el-table-column>
      <el-table-column label="成本价" width="120" align="right" prop="costPrice">
        <template #default="scope">
          {{ `￥${scope.row.costPrice}` }}
        </template>
      </el-table-column>
      <el-table-column label="单位" prop="unit" width="80" />
      <el-table-column label="描述" prop="remark" />
      <el-table-column fixed="right" align="center" label="操作" width="100" v-if="operation">
        <template #default="scope">
          <el-button
            v-if="operation == 'add'"
            type="primary"
            size="small"
            icon="plus"
            @click="handleAdd(scope.row)"
          >
            添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-if="total > 0"
      v-model:total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />
  </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from "vue";
import MaterialAPI, {
  MaterialPageVO,
  MaterialPageQuery,
  Material_LABEL,
  MaterialType,
  MaterialAllType,
} from "@/api/business/material/material.api";

const props = defineProps({
  modelValue: {
    type: Array,
    default: () => [],
  },
  // 查询参数
  exQuery: {
    type: Object,
    default: () => ({}),
  },
  // 操作类型 默认 无-勾选 add-添加数量
  operation: {
    type: String,
    default: "",
  },
  // 材料类型
  type: {
    type: String,
    default: "",
  },
  // 是否包含定额
  withNorm: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["update:modelValue", "selection-change", "add-item"]);

const total = ref(0); // 总数
const filteredMaterials = ref<MaterialPageVO[]>(); // 过滤后的材料
const checked = ref<MaterialPageVO[]>([]); // 已选择的材料
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  type: props.type as MaterialType,
}) as MaterialPageQuery;

// 勾选
const handleSelectionChange = (selection: MaterialPageVO[]) => {
  checked.value = selection;
  emit("update:modelValue", selection);
  emit("selection-change", selection);
};

// 添加按钮
const handleAdd = (row: MaterialPageVO) => {
  ElMessage.success("添加成功");
  emit("add-item", row);
};

// 获取已选列表
function getChecked(): MaterialPageVO[] {
  return checked.value || [];
}

const handleQuery = () => {
  Object.assign(queryParams, props.exQuery);
  MaterialAPI.getPage(queryParams).then((data) => {
    filteredMaterials.value = data.list;
    total.value = data.total;
  });
};

const handleResetQuery = () => {
  queryParams.keywords = "";
  queryParams.pageNum = 1;
  queryParams.type = undefined;
  handleQuery();
};

defineExpose({
  getChecked,
  handleQuery,
});
</script>

<style scoped>
.selector-wrap {
  padding: 10px;
}
</style>
